﻿using System;
using System.Threading.Tasks;
using UnityEngine;

namespace XGame
{
	public class Init: MonoBehaviour
	{
		private void Start()
		{
			Task.Run(StartAsync);
		}

		private async void StartAsync()
		{
			UnityMainThreadDispatcher.Instance().Enqueue(() =>
			{
				DontDestroyOnLoad(gameObject);
			});
			
			AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
			{
				Log.Error(e.ExceptionObject.ToString());
			};
			
			World.Instance.AddSingleton<TimeInfo>();
			
			await World.Instance.AddSingleton<ResourcesComponent>().CreatePackageAsync("DefaultPackage", true);
		}

		private void Update()
		{
			TimeInfo.Instance.Update();
			//FiberManager.Instance.Update();
		}

		private void LateUpdate()
		{
			//FiberManager.Instance.LateUpdate();
		}

		private void OnApplicationQuit()
		{
			World.Instance.Dispose();
		}
	}
	
	
}